package cn.ynrcb.personnelfile.sysadmin.controller;

import cn.ynrcb.personnelfile.common.utils.PageUtils;
import cn.ynrcb.personnelfile.common.utils.R;
import cn.ynrcb.personnelfile.sysadmin.dao.SysDictDao;
import cn.ynrcb.personnelfile.sysadmin.entity.SysDictEntity;
import cn.ynrcb.personnelfile.sysadmin.service.SysDictService;
import cn.ynrcb.personnelfile.sysadmin.vo.AddressDictVo;
import cn.ynrcb.personnelfile.sysadmin.vo.DictVo;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.DynamicParameter;
import com.github.xiaoymin.knife4j.annotations.DynamicResponseParameters;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 数据字典表
 *
 * @author wujianfan
 * @email fanxipo@qq.com
 * @date 2021-11-04 16:47:31
 */
@Api(value = "整改需求系统系统", tags = {"系统相关=>数据字典相关接口"})
@RestController
@RequestMapping("sys/sysdict")
public class SysDictController extends AbstractController{
    @Autowired
    private SysDictService sysDictService;
    @Autowired
    private SysDictDao sysDictDao;

    /**
     * 根据县区id确定一个州市
     */
    @ApiOperation("根据县区id确定一个州市")
    @ApiOperationSupport(
            responses = @DynamicResponseParameters(properties = {
                    @DynamicParameter(value = "状态码",name = "code",example = "0"),
                    @DynamicParameter(value = "信息",name = "message",example = "success"),
                    @DynamicParameter(value = "州市id",name = "zs",example = "530100")
            })
    )
    @GetMapping("/zsId")
    public R getZsIdByXqId(String xqId){
        String zsId = "";
        zsId = sysDictDao.getZsIdByXqId(xqId);
        return R.ok().put("zsId", zsId);
    }

    /**
     * 获取州市数据字典列表
     */
    @ApiOperation("获取州市数据字典列表")
    @ApiOperationSupport(
            responses = @DynamicResponseParameters(properties = {
                    @DynamicParameter(value = "状态码",name = "code",example = "0"),
                    @DynamicParameter(value = "信息",name = "message",example = "success"),
                    @DynamicParameter(value = "州市数据字典列表",name = "zsList",example = "zsList")
            })
    )
    @GetMapping("/addressZsMap")
    public R addressZsMap(){
        List<AddressDictVo> zsAddressDictList = sysDictDao.getZsAddressDataList();
        return R.ok().put("zsList", zsAddressDictList);
    }

    /**
     * 获取县区数据字典列表
     */
    @ApiOperation("获取县区数据字典列表")
    @ApiOperationSupport(
            responses = @DynamicResponseParameters(properties = {
                    @DynamicParameter(value = "状态码",name = "code",example = "0"),
                    @DynamicParameter(value = "信息",name = "message",example = "success"),
                    @DynamicParameter(value = "县区数据字典列表",name = "xqList",example = "xqList")
            })
    )
    @GetMapping("/addressXqMap")
    public R addressXqMap(String id){
        List<AddressDictVo> xqAddressDictList = new ArrayList<>();
        if(StringUtils.isNotEmpty(id)){
            xqAddressDictList = sysDictDao.getXqAddressDataListByPid(id);
        }else{
            xqAddressDictList = sysDictDao.getXqAddressDataList();
        }
        return R.ok().put("xqList", xqAddressDictList);
    }

    /**
     * 获取地区数据字典树
     */
    @ApiOperation("获取地区数据字典树")
    @ApiOperationSupport(
            responses = @DynamicResponseParameters(properties = {
                    @DynamicParameter(value = "状态码",name = "code",example = "0"),
                    @DynamicParameter(value = "信息",name = "message",example = "success"),
                    @DynamicParameter(value = "地区数据字典树",name = "list",example = "list")
            })
    )
    @GetMapping("/address")
    public R addressTree(){
        String id = "1";
        List<AddressDictVo> addressDictList = new ArrayList<>();
        addressDictList = sysDictService.getAddressDataList(addressDictList,id);
        return R.ok().put("list", addressDictList);
    }

    /**
     * 获取地区数据字典list
     */
    @ApiOperation("获取地区数据字典list")
    @ApiOperationSupport(
            responses = @DynamicResponseParameters(properties = {
                    @DynamicParameter(value = "状态码",name = "code",example = "0"),
                    @DynamicParameter(value = "信息",name = "message",example = "success"),
                    @DynamicParameter(value = "地区数据字典list",name = "list",example = "list")
            })
    )
    @GetMapping("/addresslist")
    public R addressList(){
        List<AddressDictVo> addressDictList = sysDictService.getAddressList();
        return R.ok().put("list", addressDictList);
    }

    /**
     * 分类获取数据字典列表
     */
    @ApiOperation("分类获取数据字典列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "params", value = "参数",example ="params", required = false),
            @ApiImplicitParam(name = "module", value = "应用模块",example ="enterprise", required = false),
            @ApiImplicitParam(name = "type", value = "分类",example ="a05-1", required = false)
    })
    @ApiOperationSupport(
            responses = @DynamicResponseParameters(properties = {
                    @DynamicParameter(value = "状态码",name = "code",example = "0"),
                    @DynamicParameter(value = "信息",name = "message",example = "success"),
                    @DynamicParameter(value = "数据字典map",name = "dictMap",example = "dictMap")
            })
    )
    @GetMapping("/data")
//    @RequiresPermissions("sys:sysdict:data")
    public R data(@RequestParam Map<String, Object> params){
        Map<String,List<DictVo>> dictMap = sysDictService.getDataList(params);

        return R.ok().put("dictMap", dictMap);
    }

    /**
     * 数据字典列表分页列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:sysdict:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = sysDictService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:sysdict:info")
    public R info(@PathVariable("id") Long id){
		SysDictEntity sysDict = sysDictService.getById(id);

        return R.ok().put("sysDict", sysDict);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("sys:sysdict:save")
    public R save(@RequestBody SysDictEntity sysDict){
		sysDictService.save(sysDict);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("sys:sysdict:update")
    public R update(@RequestBody SysDictEntity sysDict){
		sysDictService.updateById(sysDict);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("sys:sysdict:delete")
    public R delete(@RequestBody Long[] ids){
		sysDictService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
